package io.renren.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.TreeMap;

/**
 * 获取视频(MP4类型)
 *
 * @author Zzh
 */
public class CatchVideo2 {

    /**
     * 视频名称
     */
    private static String videoName;

    /**
     * 视频前缀
     */
    private static String videoPathPrefix;

    /**
     * 设置日期格式
     */
    private static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 主程序
     *
     * @param args
     */
    public static void main(String[] args) {
        String path = "/Users/keyyang/zhuanhua";
        Map<Integer, File> map = new TreeMap<>();
        File file = new File(path);        //获取其file对象
        File[] fs = file.listFiles();    //遍历path下的文件和目录，放在File数组中
        for (File f : fs) {                    //遍历File[]数组
            if (!f.isDirectory())        //若非目录(即文件)，则打印
            {
                Integer no = Integer.valueOf(f.getName().split("[-]")[1].split("[.]")[0]);
                map.put(no, f);
            }
        }
        for (Map.Entry<Integer,File> entry:map.entrySet()){
            System.out.println(entry.getKey());
        }


        CatchVideo2.composeFile("/usr/local/nginx/html/ybjituanWebPic/清浦新闻.mp4",map);

    }

    /**
     * 合并文件
     * @param fileOutPath
     * @param fileMap
     */
    public static void composeFile(String fileOutPath, Map<Integer, File> fileMap) {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(new File(fileOutPath));
            byte[] bytes = new byte[1024];
            int length = 0;
            for (Map.Entry<Integer,File> entry:fileMap.entrySet()){
                File file = entry.getValue();
                if (!file.exists())
                    continue;
                FileInputStream fis = new FileInputStream(file);
                while ((length = fis.read(bytes)) != -1) {
                    fileOutputStream.write(bytes, 0, length);
                }
            }

        } catch (Exception e) {
            System.out.println("视频合成失败");
        }
    }

}
